package com.ostrich.note;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.ostrich.note.utils.DialogUtils;

import java.io.File;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        dd();
    }

    public void dd() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                String ACCESS_KEY =  "AKIATPTD6DXMOXDCBWNR";
                String SECRET_KEY = "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";
                String MY_BUCKET =  "picture-ios";
                String OBJECT_KEY = "defaultKey";
                File UPLOADING_IMAGE = new File( "/sdcard/Pictures/笔记.png");
                AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
                AmazonS3 s3 = new AmazonS3Client(credentials);
                java.security.Security.setProperty("networkaddress.cache.ttl" ,"60");
                s3.setRegion(Region.getRegion(Regions.SA_EAST_1));
                s3.setEndpoint("https://s3-sa-east-1.amazonaws.com/");
                List<Bucket> buckets=s3.listBuckets();
                for(Bucket bucket:buckets){
                    Log.e("Bucket","Name"+bucket.getName()+" Owner"+bucket.getOwner()+" Date" + bucket.getCreationDate());
                }
                Log.e("Size","" + s3.listBuckets().size());
                TransferUtility transferUtility = new TransferUtility(s3, getApplicationContext());

                TransferObserver observer = transferUtility.upload(MY_BUCKET,OBJECT_KEY,UPLOADING_IMAGE);
                observer.setTransferListener(new TransferListener() {
                    @Override
                    public void onStateChanged(int id, TransferState state) {
                        // do something
//                progress.hide();
//                path.setText("ID"+id+"\
//                        State"+state.name()+"\
//                        Image ID"+OBJECT_KEY);
                    }

                    @Override
                    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
//                int percentage = (int) (bytesCurrent / bytesTotal * 100);
//                progress.setProgress(percentage);
//                //Display percentage transfered to user
                    }

                    @Override
                    public void onError(int id, Exception ex) {
//                // do something
//                Log.e("Error ",""+ex );
                    }

                });
            }
        }).start();


    }
}